﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.IO;

public partial class CMS_Default : System.Web.UI.Page
{
    Usas.BLL.Cms.cmsType ct = new Usas.BLL.Cms.cmsType();
    Usas.Common.Cms.CurrentInfo cinfo = new Usas.Common.Cms.CurrentInfo();
    Usas.Common.Cms.Role ro = new Usas.Common.Cms.Role();
    Usas.BLL.Cms.AspPager ap = new Usas.BLL.Cms.AspPager();
    Usas.BLL.Cms.cms_block_setting cbs = new Usas.BLL.Cms.cms_block_setting();
    Usas.Model.Cms.cms_block blm = new Usas.Model.Cms.cms_block();
    Usas.BLL.Cms.cms_block bl = new Usas.BLL.Cms.cms_block();
    Usas.BLL.Cms.StatOperate so = new Usas.BLL.Cms.StatOperate();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            try
            {
                CMS_MasterPage_Layout mmc = (CMS_MasterPage_Layout)this.Page.Master;
                mmc.ManagerHover();
                //mmc.LeftExist = false;
                DataTable dt = cbs.GetList("s_page='Default.aspx'").Tables[0];
                LayoutBind(dt.Select("s_location like '1,%'", "s_location asc"), true);
                LayoutBind(dt.Select("s_location like '2,%'", "s_location asc"), false);

                //记录流量
                so.GlobalStat(Request.UserHostAddress, cinfo.GetEmpId(), DateTime.Now.Year.ToString(),
                    DateTime.Now.Month.ToString(), DateTime.Now.Day.ToString(), so.GetBrowserVersion(this.Request), so.GetOSNameByUserAgent(Request.UserAgent));

            }
            catch { }
        }
    }

    private void LayoutBind(DataRow[] dr, bool isleft)
    {
        for (int i = 0; i < dr.Length; i++)
        {
            Panel pa = isleft ? Left : Right;
            blm = bl.GetModel(Convert.ToInt32(dr[i]["s_b_id"]));
            switch (blm.b_path)
            {
                case "CacheView.ascx":
                    ViewBind(pa, blm.b_id);
                    break;
                default:
                    pa.Controls.Add(Page.LoadControl("~/CMS/Control/" + blm.b_path));
                    break;
            }
        }
    }

    private void ViewBind(Panel pa, int id)
    {

        Usas.BLL.Cms.cms_block_common_detail cbcd = new Usas.BLL.Cms.cms_block_common_detail();
        Usas.Model.Cms.cms_block_common_detail cbcdm = new Usas.Model.Cms.cms_block_common_detail();
        DataSet ds = cbcd.GetList("c_b_id=" + id + " and c_type='Default.aspx'");

        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            if (ds.Tables[0].Rows[0]["c_value"].ToString() == "1")
            {
                id = 1;
            }
            else
            {
                id = 2;
            }
        }
        else
        {
            id = 2;
        }

        Panel wc = new Panel();
        wc.CssClass = "Welcom";
        Panel ViewContent1 = new Panel();
        ViewContent1.ID = "ViewContent1";
        ViewContent1.CssClass = "groupWrapper";
        Panel ViewContent2 = new Panel();
        ViewContent2.ID = "ViewContent2";
        ViewContent2.CssClass = "groupWrapper";

        if (id == 1)
        {
            ViewContent1.Attributes.Add("style", "width:96%");
        }
        else
        {
            ViewContent1.Attributes.Add("style", "width:48%");
            ViewContent2.Attributes.Add("style", "width:48%");
        }



        DataTable dt = ap.GetTypeTableBySetting(ro.GetSelfSettingRole(), "Del <> 1", 1, -1, "TypeOrder");


        //循环遍历内容
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CMS_Control_CacheView ccc = (CMS_Control_CacheView)Page.LoadControl("~/CMS/Control/CacheView.ascx");
            bool edit = Convert.ToInt32(dt.Rows[i]["edit"]) > 0 ? true : false;
            bool view = Convert.ToInt32(dt.Rows[i]["view"]) > 0 ? true : false;

            if (view)
            {
                string img = "";
                if (id == 1)
                {
                    string temp = System.Configuration.ConfigurationManager.AppSettings["UsasInformationPath"].ToString() + "Pic\\";

                    FileInfo fn = new FileInfo(temp + dt.Rows[i]["ImgFile"].ToString());
                    if (fn.Exists)
                    {
                        img = i % 2 == 0 ? "l," + System.Configuration.ConfigurationManager.AppSettings["UsasInformationIIS"].ToString() + "Pic/" + fn.Name
                            : "r," + System.Configuration.ConfigurationManager.AppSettings["UsasInformationIIS"].ToString() + "Pic/" + fn.Name;
                    }

                }
                ccc.GetCacheView(CMS_Control_CacheView.ShowMode.Type, 9, dt.Rows[i]["TypeId"].ToString(), cinfo.GetEmpRole(), dt.Rows[i]["TypeName"].ToString(), 1, "", edit, false, 10, img);
                if (id == 1)
                {
                    ViewContent1.Controls.Add(ccc);
                }
                else
                {
                    if (i % 2 == 0)
                    {
                        ViewContent1.Controls.Add(ccc);
                    }
                    else
                    {
                        ViewContent2.Controls.Add(ccc);
                    }
                }

            }

        }

        wc.Controls.Add(ViewContent1);
        wc.Controls.Add(ViewContent2);

        pa.Controls.Add(wc);

    }


}

